package czk.concrent.threadpool.method;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/7/5 17:58
 */
public class ShutDownNow {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        // 提交三个任务
        executorService.submit(()->{
            System.out.println("执行任务一");
            try {
                Thread.sleep(1*1000);
                System.out.println("任务一执行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            System.out.println("执行任务二");
            try {
                Thread.sleep(1*1000);
                System.out.println("任务二执行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务三需要等想爱你诚意或者线程二执行完毕后再执行
        executorService.submit(()->{
            System.out.println("执行任务三");
            try {
                Thread.sleep(1*1000);
                System.out.println("任务三执行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 结束线程池
        System.out.println("关闭线程池");
        // shutdown并不会阻塞主线程的执行
        executorService.shutdownNow();
        // 需要执行等待时间的话用这个方法，但是其并不会结束线程
        //executorService.awaitTermination(8, TimeUnit.SECONDS);
        System.out.println("主线程并未结束");

    }
}
